$(document).ready(function() {
  var displayChartCount = 0;
  var page = new Page();
  // $("#main #chartList ul li").click(function() {
  function f10ClearFnc(){
    var chart = new Chart();
     chart.initializeCanvas("chart", 1222, 550);
     displayChartCount = 0;
     page = new Page();
     $("input").val("");
  }
  $("#f10_btn").click(function(){
  	 f10ClearFnc();
  })
  $("#f5_btn").click(function(){
    if( page.chartCount <=0 ){
          showErrorDialog("003","请选择需要管制图后再计算");
          return false;
    }
    var calChart ;
    for(var i=0;i<page.chartCount;i++){
      if( page.chartList[i].chart_typ=="01"||
          page.chartList[i].chart_typ=="10"){
        calChart = page.chartList[i];
        break;
      }
    }
    calChart.caclCPk();
    box.showDialog(calChart);
  	// showSuccessDialog(calChart.mean);
  })
  var box = {};
  box.showDialog = function(calChart){
    $('#calCpkDialog').modal({
        backdrop:true,
        keyboard:false,
        show:false
    });
    $('#calCpkDialog').unbind('shown.bs.modal');
    $('#calCpkDialog').bind('shown.bs.modal',function(){
       box.initDialog(calChart); 
    });
    $('#calCpkDialog').modal("show");

  }
  box.initDialog = function(calChart){
    $("#calCpkDialogForm input").val("");
    $("#meanTxt").val(calChart.mean);
    $("#sigmaTxt").val(calChart.sigma);
    $("#caTxt").val(calChart.ca);
    $("#cpTxt").val(calChart.cp);
    $("#cpkTxt").val(calChart.cpk);
    $("#ppTxt").val(calChart.pp);
    $("#ppkTxt").val(calChart.ppk);
    $("#gradeTxt").val(calChart.grade);
  }

   function f1QueryFnc(){

    /*** 将Div实例化为modal窗体 ***/
    function diaLogQueryFnc(){
       var selectRowID = $("#chartListGrd").jqGrid("getGridParam","selrow");
       var rowData = $("#chartListGrd").jqGrid("getRowData",selectRowID);
       showChart(rowData);
       $('#queryDialog').modal("hide");

    }
    function queryDialogShowFnc(){
       $("input").val("");
       $("select").empty();
       var itemInfoCM = [
          {name: 'col_typ_fk'     , index: 'col_typ_fk'   ,  label: "col_typ_fk"   , width: 80 ,hidden:true},
          {name: 'grp_no_fk'      , index: 'grp_no_fk'    ,  label: "grp_no_fk"    , width: 80 ,hidden:true},
          {name: 'chart_no'       , index: 'chart_no'     ,  label: "chart_no"     , width: 80 ,hidden:true},
          {name: 'chart_name'     , index: 'chart_name'   ,  label: "管制图名称"   , width: 130 },
          {name: 'chart_dsc'      , index: 'chart_dsc'    ,  label: "管制图描述"   , width: 130 }
        ];
        $("#chartListGrd").jqGrid({
            url:"",
            datatype:"local",
            mtype:"POST",
            // height:$("#chartListDiv").height(),
            // height:400,//TODO:需要根据页面自适应，要相对很高
            // width:$("#chartListDiv").width(),
            autowidth:false,//宽度根据父元素自适应:true
            shrinkToFit:false,
            scroll:false,
            resizable : true,
            loadonce:true,
            // toppager: true , 翻页控件是否在上层显示
            fixed: true,
            // hidedlg:true,
            jsonReader : {
                  // repeatitems: false
                },
            viewrecords : true, //显示总记录数
            rownumbers  :true ,//显示行号
            rowNum:15,         //每页多少行，用于分页
            rownumWidth : 20,  //行号列宽度
            emptyrecords :true ,
            colModel: itemInfoCM,
            gridComplete:function(){

            },
            ondblClickRow: function (rowid,iRow,iCol,e){
              var rowData = $(this).jqGrid("getRowData",rowid);
              showChart(rowData);
              $('#queryDialog').modal("hide");
            }
          })
      var grpMain ={
        // col_typ :"2"
        // grp_no  : "00001",
        // grp_dsc : "XIN TEST"
      };
      // var grp_name = $("#queryGroupDialog_groupNameTxt").val();
      // if(grp_name!=""){
      //   grpMain.grp_name = grp_name ;
      // }
      var url = "spc/findSPCGroupMain.action" ;

      var outTrxObj = comSendSpcJson(url,grpMain);
      if( outTrxObj.rtn_code == 0 ){
        // setGridInfo(outTrxObj.oary,"#groupListGrd");
        for(var i=0;i<outTrxObj.oary.length;i++){
          $("#queryDialog_grpNameSel").append("<option value="+outTrxObj.oary[i].grp_no +">" +outTrxObj.oary[i].grp_name +"</option>");
        }
        $("#queryDialog_grpNameSel").select2({
 	    	theme : "bootstrap"
 	    });
        grpNameChangeFnc();
      }
    }
    function grpNameChangeFnc(){
      var grp_no_fk = $("#queryDialog_grpNameSel").val();
      var url = "spc/findChartMain.action" ;
      var chartObj ={
        grp_no_fk : grp_no_fk
      };
      var outTrxObj = comSendSpcJson(url,chartObj);
      if( outTrxObj.rtn_code == 0 ){
        setGridInfo(outTrxObj.oary,"#chartListGrd");
      }
    }
    $('#queryDialog').modal({
        backdrop:true,
        keyboard:false,
        show:false
    });
    $('#queryDialog').unbind('shown.bs.modal');
    $("#queryDialog_queryBtn").unbind('click');
    $("#queryDialog_grpNameSel").unbind("change");
    $('#queryDialog').bind('shown.bs.modal',queryDialogShowFnc);
    $('#queryDialog').modal("show");
    $("#queryDialog_queryBtn").bind('click',diaLogQueryFnc);
    $("#queryDialog_grpNameSel").bind("change",grpNameChangeFnc);
  }

  function showChart(selectChartGridData){
    var querySettingObj ={
      col_typ  : selectChartGridData.col_typ_fk,
      grp_no   : selectChartGridData.grp_no_fk,
      chart_no : selectChartGridData.chart_no
    };
    var url = "spc/findChartSetting.action" ;
    var outTrxObj = comSendSpcJson(url,querySettingObj);
    var chartSettingOary=outTrxObj.oary;
    var dataArray = new Array();
    for(var i=0;i<chartSettingOary.length;i++){
//  for(var i=0;i<1;i++){
      var data = new Object();
      data.upl = chartSettingOary[i].chart_upl;
      data.usl = chartSettingOary[i].chart_usl;
      data.ucl = chartSettingOary[i].chart_ucl;
      data.target = chartSettingOary[i].chart_target;
      data.lcl = chartSettingOary[i].chart_lcl;
      data.lsl = chartSettingOary[i].chart_lsl;
      data.lpl = chartSettingOary[i].chart_lpl;
      data.cname = chartSettingOary[i].chart_name;
      data.ctitle = chartSettingOary[i].chart_dsc;
      data.chart_typ = chartSettingOary[i].chart_typ_fk;
      data.gname = chartSettingOary[i].grp_name;
      
      data.point = new Array();
      //查询 chart_data
      var queryObj2 ={
        col_typ_fk  : selectChartGridData.col_typ_fk,
        grp_no_fk   : selectChartGridData.grp_no_fk ,
        chart_no_fk : selectChartGridData.chart_no  ,
        chart_typ_fk: chartSettingOary[i].chart_typ_fk
     };
    var url = "spc/findChartData.action" ;
    var outTrxObj2 = comSendSpcJson(url,queryObj2);
    if( outTrxObj2.rtn_code != 0 ){
      //TODO
      alert("查询失败");
      return false ;
    }
    
      var dataList = outTrxObj2.oary ;
      for ( var j = 0; j < dataList.length; j++) {
        var point = new Object();
        point.inputTimestamp = dataList[j].input_timestamp;
        // point.value = Math.ceil(Math.random() * 200 + 150);
        point.value = dataList[j].chart_value;
        point.glassId = dataList[j].prd_seq_id_fk;
        point.processEqptId =dataList[j].p_tool_id_fk;
        point.measurementEqptId = dataList[j].m_tool_id_fk;
        point.comment = dataList[j].chart_comment;
        //
        if(chartSettingOary[i].chart_typ_fk=="01"){
	        point.rawData=dataList[j].rawData
	        
        }
        data.point.push(point);
      }
  
      dataArray.push(data);
      displayChartCount++;
    }
    $(this).toggleClass("chartListSelectedBackgroundColor");

    switch (displayChartCount) {

    case 1:
      var chart = new Chart();
      chart.initializeCanvas("chart", 1222, 550);
      chart.initializeChart(dataArray[0], 1, 1);
      chart.drawPoint();
      chart.activateClickEvent();
      break;

    case 2:
      var chart1 = new Chart();
      chart1.initializeCanvas("chart", 1222, 550);
      chart1.initializeChart(dataArray[0], 2, 1);
      chart1.drawPoint();
//      chart1.activateClickEvent();
      
      //fix bug
      var chart2 = new Chart();
      chart2.initializeCanvas("chart", 1222, 550,true);
      chart2.initializeChart(dataArray[1], 2, 2);
      chart2.drawPoint();
//      chart2.activateClickEvent();
      page.addChart(chart1);
      page.addChart(chart2);
      page.activateClickEvent();
      break;

    case 3:
      var chart1 = new Chart();
      chart1.initializeCanvas("chart", 1222, 550);
      chart1.initializeChart(dataArray[0], 3, 1);
      chart1.drawPoint();

      var chart2 = new Chart();
      chart2.initializeCanvas("chart", 1222, 550);
      chart2.initializeChart(dataArray[1], 3, 2);
      chart2.drawPoint();

      var chart3 = new Chart();
      chart3.initializeCanvas("chart", 1222, 550);
      chart3.initializeChart(dataArray[2], 3, 3);
      chart3.drawPoint();

      page.addChart(chart1);
      page.addChart(chart2);
      page.addChart(chart3);
      page.activateClickEvent();
      break;
    }
  }
  $("#f1_btn").click(function(){
   f1QueryFnc();
  });
})